<?php

class Relationshipscontroller{

	/**
	 * Controller constructor - direct call to false when being embedded via another controller
	 * @param Registry $registry our registry
	 * @param bool $directCall - are we calling it directly via the framework (true), or via another controller (false)
	 */
	public function __construct( Registry $registry, $directCall )
	{
		$this->registry = $registry;

		$urlBits = $this->registry->getObject('url')->getURLBits();
		if( isset( $urlBits[1] ) )
		{
			switch( $urlBits[1] )
			{
				case 'pending':
					$this->pendingRelationships();
					break;
				case 'list':
					{
						$offset = 1;
							
						if (isset($_POST['page_num']))
						$offset = $_POST['page_num'];

						$this->myRelationships($offset);
					}
					break;
				case 'search':
					{
						if (isset($_POST['searchword']))
						{
							$searchkey = $_POST['searchword'];
							$this->searchFriends($searchkey);
						}
						else
						$this->myRelationships(1);
					}
					break;
				default:
					$this->myRelationships(1);
					break;
			}

		}
		else
		{
			$this->myRelationships(1);
		}

	}

	private function myRelationships($page)
	{
		$profileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
		$userid = $this->registry->getObject('authenticate')->getUser()->getUserID();

		if ($page <= 0)
			$page = 1;
			
		$refresh = -1;
		if (isset($_POST['refresh']))
			$refresh = $_POST['refresh'];

		if ($refresh == 0)
			$this->registry->getObject('template')->buildFromTemplates('friends/friends_header.tpl.php', 'friends/list_friends.tpl.php', 'friends/friends_footer.tpl.php');
		else if ($refresh == 1)
			$this->registry->getObject('template')->buildFromTemplates('friends/list_friends.tpl.php', 'friends/friends_footer.tpl.php');
		else
			$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php','friends/friends_header.tpl.php',
				'friends/list_friends.tpl.php', 'friends/friends_footer.tpl.php', 'main/footer.tpl.php');


		require_once( FRAMEWORK_PATH . 'models/relationships.php');
		$relationships = new Relationships( $this->registry );
		$loadLimit = 1;
		$loadedRows = 0;

		$offset = ($page - 1)*($relationships->limitPerPage);
		
	
		$cache = $relationships->getFriends( $offset, $profileid, $userid, $loadLimit );

		while ( ($loadedRows < 25 && $relationships->loadedRows > 0))
		{
			$this->registry->getObject('template')->getPage()->addPPTag('row_id', $loadedRows);
			$this->registry->getObject('template')->addTemplateBit('friend_row'.$loadedRows, 'friends/list_friends_row.tpl.php');
			$this->registry->getObject('template')->getPage()->addPPTag( 'friends'.$loadedRows, array( 'DATA', $cache ) );
			$this->registry->getObject('template')->parseOutput();

			++$loadedRows;
			$next = "{friend_row{row_id}}";
			$this->registry->getObject('template')->getPage()->addPPTag('next_row', $next);
			$this->registry->getObject('template')->parseOutput();


			$offset+=$relationships->loadedRows;

			$cache = $relationships->getFriends( $offset, $profileid, $userid, $loadLimit );

		}

		//if ($refresh != 1)
		//{
		$pagenums = array();

		for($pagenum = 0; $pagenum < $relationships->totalPages; ++$pagenum)
		{
			$number['page_num'] = $pagenum + 1;
			$pagenums[] = $number;
		}

		$pagecache = $this->registry->getObject('db')->cacheData( $pagenums );
		$this->registry->getObject('template')->getPage()->addTag('pagenumbers', array('DATA', $pagecache));
		//}

		$this->registry->getObject('template')->getPage()->addTag('friend_row{row_id}', '');

		$this->registry->getObject('template')->getPage()->addTag( 'page_number', $page );
		$this->registry->getObject('template')->getPage()->addTag( 'num_pages', $relationships->totalPages);

		unset ($_POST['refresh']);
	}

	private function searchFriends( $name )
	{
		require_once( FRAMEWORK_PATH . 'models/relationships.php');
		$relationships = new Relationships( $this->registry );

		$fullsearch = 0;
		if (isset($_POST['fullsearch']))
		$fullsearch = $_POST['fullsearch'];
			
		unset ($_POST['fullsearch']);

		$this->registry->getObject('template')->buildFromTemplates('friends/friend_search_result.tpl.php');

		$profileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
		$userid = $this->registry->getObject('authenticate')->getUser()->getUserID();

		$cache = $relationships->search($name, $profileid, $userid, $fullsearch);

		$this->registry->getObject('template')->getPage()->addTag( 'friends', array( 'DATA', $cache ) );

		$this->registry->getObject('template')->parseOutput();
	}

	private function pendingRelationships()
	{
		if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			$profileid = $this->registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
			$userid = $this->registry->getObject('authenticate')->getUser()->getUserID();

			require_once( FRAMEWORK_PATH . 'models/relationships.php');
			$relationships = new Relationships( $this->registry );
			$pending = $relationships->getPendingRequests($userid, $profileid);
			$this->registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php',
				'friends/pending.tpl.php', 'main/footer.tpl.php');
			
			$this->registry->getObject('template')->getPage()->addTag('pending', array( 'SQL', $pending ) );
		}
		else
		{
			$this->registry->errorPage( 'Please login', 'Please login to manage pending connections');
		}
	}
}
?>